home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / PrPrimerVol1 / (CH. 4) Events / EventTutor.c next >
Encoding:
C/C++ Source or Header  |  1990-01-11  |  8.1 KB  |  358 lines  |  [TEXT/KAHL]

  1. #define BASE_RES_ID            400
  2. #define NIL_POINTER            0L
  3. #define MOVE_TO_FRONT        -1L
  4. #define REMOVE_ALL_EVENTS    0
  5.  
  6. #define LEAVE_WHERE_IT_IS    FALSE
  7. #define NORMAL_UPDATES        TRUE
  8.  
  9. #define SLEEP                0L
  10. #define NIL_MOUSE_REGION    0L
  11. #define WNE_TRAP_NUM        0x60
  12. #define UNIMPL_TRAP_NUM        0x9F
  13. #define SUSPEND_RESUME_BIT    0x0001
  14. #define ACTIVATING            1
  15. #define RESUMING            1
  16.  
  17. #define TEXT_FONT_SIZE        12
  18.  
  19. #define DRAG_THRESHOLD        30
  20. #define MIN_WINDOW_HEIGHT    50
  21. #define MIN_WINDOW_WIDTH    50
  22. #define SCROLL_BAR_PIXELS    16
  23.  
  24. #define ROWHEIGHT            15
  25. #define LEFTMARGIN            10
  26. #define STARTROW            0
  27. #define HORIZONTAL_OFFSET    0
  28.  
  29. PicHandle        gPictureHandle;
  30. WindowPtr        gPictWindow, gEventWindow;
  31. Boolean            gDone, gWNEImplemented;
  32. EventRecord        gTheEvent;
  33. int                gCurRow, gMaxRow;
  34. Rect            gDragRect, gSizeRect;
  35.  
  36. /******************************** main *********/
  37.  
  38. main()
  39. {
  40.     ToolBoxInit();
  41.     WindowInit();
  42.     LoadPicture();
  43.     SetUpDragRect();
  44.     SetUpSizeRect();
  45.     
  46.     MainLoop();
  47. }
  48.  
  49. /*********************************** ToolBoxInit */
  50.  
  51. ToolBoxInit()
  52. {
  53.     InitGraf( &thePort );
  54.     InitFonts();
  55.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  56.     InitWindows();
  57.     InitMenus();
  58.     TEInit();
  59.     InitDialogs( NIL_POINTER );
  60.     InitCursor();
  61. }
  62.  
  63. /******************************** WindowInit *********/
  64.  
  65. WindowInit()
  66. {
  67.     gPictWindow = GetNewWindow( BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT );
  68.     gEventWindow = GetNewWindow( BASE_RES_ID+1, NIL_POINTER, MOVE_TO_FRONT );
  69.     
  70.     SetPort( gEventWindow );
  71.     SetupEventWindow();
  72.     
  73.     ShowWindow( gEventWindow );
  74.     ShowWindow( gPictWindow );
  75.     
  76.     SelectWindow( gEventWindow );
  77. }
  78.  
  79. /******************************** SetupEventWindow *********/
  80.  
  81. SetupEventWindow()
  82. {
  83.     Rect    eventRect;
  84.     
  85.     eventRect = gEventWindow->portRect;
  86.     gMaxRow = eventRect.bottom - eventRect.top - ROWHEIGHT;
  87.     gCurRow = STARTROW;
  88.     
  89.     TextFont( monaco );
  90.     TextSize( TEXT_FONT_SIZE );
  91. }
  92.  
  93. /******************************** LoadPicture *********/
  94.  
  95. LoadPicture()
  96. {
  97.     gPictureHandle = GetPicture( BASE_RES_ID );
  98. }
  99.  
  100. /******************************** SetUpDragRect *********/
  101.  
  102. SetUpDragRect()
  103. {
  104.     gDragRect = screenBits.bounds;
  105.     gDragRect.left += DRAG_THRESHOLD;
  106.     gDragRect.right -= DRAG_THRESHOLD;
  107.     gDragRect.bottom -= DRAG_THRESHOLD;
  108. }
  109.  
  110. /******************************** SetUpSizeRect *********/
  111.  
  112. SetUpSizeRect()
  113. {
  114.     gSizeRect.top = MIN_WINDOW_HEIGHT;
  115.     gSizeRect.left = MIN_WINDOW_WIDTH;
  116.     
  117.     gSizeRect.bottom = screenBits.bounds.bottom - screenBits.bounds.top;
  118.     gSizeRect.right = screenBits.bounds.right - screenBits.bounds.left;
  119. }
  120.  
  121. /******************************** MainLoop *********/
  122.  
  123. MainLoop()
  124. {
  125.     gDone = FALSE;
  126.     gWNEImplemented = ( NGetTrapAddress( WNE_TRAP_NUM, ToolTrap ) != NGetTrapAddress( UNIMPL_TRAP_NUM, ToolTrap ) );
  127.     while ( gDone == FALSE )
  128.         {
  129.         HandleEvent();
  130.         }
  131. }
  132.  
  133. /************************************* HandleEvent  */
  134.  
  135. HandleEvent()
  136. {
  137.     if ( gWNEImplemented )
  138.         WaitNextEvent( everyEvent, &gTheEvent, SLEEP, NIL_MOUSE_REGION );
  139.     else
  140.         {
  141.         SystemTask();
  142.         GetNextEvent( everyEvent, &gTheEvent );
  143.         }
  144.  
  145.     switch ( gTheEvent.what )
  146.         {
  147.             case nullEvent:
  148.                 /*DrawEventString( "\pnullEvent" );*/
  149.                 /*    Uncomment the previous line for a burst of flavor! */
  150.                 break;
  151.             case mouseDown:
  152.                 DrawEventString( "\pmouseDown" );
  153.                 HandleMouseDown();
  154.                 break;
  155.             case mouseUp:
  156.                 DrawEventString( "\pmouseUp" );
  157.                 break;
  158.             case keyDown:
  159.                 DrawEventString( "\pkeyDown" );
  160.                 break;
  161.             case keyUp:
  162.                 DrawEventString( "\pkeyUp" );
  163.             case autoKey:
  164.                 DrawEventString( "\pautoKey" );
  165.             case updateEvt:
  166.                 if ( (WindowPtr)gTheEvent.message == gPictWindow )
  167.                     {
  168.                     DrawEventString( "\pupdateEvt: gPictWindow" );
  169.                     BeginUpdate( gTheEvent.message );
  170.                     DrawMyPicture( gTheEvent.message, gPictureHandle );
  171.                     EndUpdate( gTheEvent.message );
  172.                     }
  173.                 else
  174.                     {
  175.                     DrawEventString( "\pupdateEvt: gEventWindow" );
  176.                     BeginUpdate( gTheEvent.message );
  177.                     /*
  178.                     *        We won't handle updates to gEventWindow,
  179.                     * but we still need to empty the gEventWindow
  180.                     * Update Region so the Window Manager will stop
  181.                     * queueing UpdateEvts.
  182.                     *        We do this with calls to BeginUpdate()
  183.                     * and EndUpdate().
  184.                     */
  185.                     EndUpdate( gTheEvent.message );
  186.                     }
  187.                 break;
  188.             case diskEvt:
  189.                 DrawEventString( "\pdiskEvt" );
  190.                 break;
  191.             case activateEvt:
  192.                 if ( (WindowPtr)gTheEvent.message == gPictWindow )
  193.                     {
  194.                     DrawGrowIcon( gTheEvent.message );
  195.                     if ( ( gTheEvent.modifiers & activeFlag ) == ACTIVATING )
  196.                         DrawEventString("\pactivateEvt: activating gPictWindow" );
  197.                     else
  198.                         DrawEventString("\pactivateEvt: deactivating gPictWindow" );
  199.                     }
  200.                 else
  201.                     {
  202.                     if ( ( gTheEvent.modifiers & activeFlag ) == ACTIVATING )
  203.                         DrawEventString( "\pactivateEvt: activating gEventWindow" );
  204.                     else
  205.                         DrawEventString( "\pactivateEvt: deactivating gEventWindow" );
  206.                     }
  207.                 break;
  208.             case networkEvt:
  209.                 DrawEventString( "\pnetworkEvt" );
  210.                 break;
  211.             case driverEvt:
  212.                 DrawEventString( "\pdriverEvt" );
  213.                 break;
  214.             case app1Evt:
  215.                 DrawEventString( "\papp1Evt" );
  216.                 break;
  217.             case app2Evt:
  218.                 DrawEventString( "\papp2Evt" );
  219.                 break;
  220.             case app3Evt:
  221.                 DrawEventString( "\papp3Evt" );
  222.                 break;
  223.             case app4Evt:
  224.                 if ( (gTheEvent.message & SUSPEND_RESUME_BIT) == RESUMING )
  225.                     DrawEventString( "\pResume event" );
  226.                 else
  227.                     DrawEventString( "\pSuspend event" );
  228.                 break;
  229.             }
  230. }
  231.  
  232. /**********************************  DrawEventString *******/
  233.  
  234. DrawEventString( s )
  235.  
  236. Str255 s;
  237.  
  238. {
  239.     if ( gCurRow > gMaxRow )
  240.         ScrollWindow();
  241.     else
  242.         gCurRow += ROWHEIGHT;
  243.         
  244.     MoveTo( LEFTMARGIN, gCurRow );
  245.     DrawString(s);
  246. }
  247.  
  248. /**********************************  ScrollWindow *******/
  249.  
  250. ScrollWindow()
  251. {
  252.     RgnHandle    tempRgn;
  253.     
  254.     tempRgn = NewRgn();
  255.     ScrollRect( &gEventWindow->portRect, HORIZONTAL_OFFSET, -ROWHEIGHT, tempRgn );
  256.     DisposeRgn( tempRgn );
  257. }
  258.  
  259. /************************************* HandleMouseDown */
  260.  
  261. HandleMouseDown()
  262. {
  263.     WindowPtr    whichWindow;
  264.     short int    thePart;
  265.     long        windSize;
  266.     GrafPtr        oldPort;
  267.     
  268.     thePart = FindWindow( gTheEvent.where, &whichWindow );
  269.     switch ( thePart )
  270.         {
  271.         case inSysWindow :
  272.             SystemClick( &gTheEvent, whichWindow );
  273.             break;
  274.         case inDrag :
  275.             DragWindow( whichWindow, gTheEvent.where, &gDragRect );
  276.             break;
  277.         case inContent:
  278.             SelectWindow( whichWindow );
  279.             break;
  280.         case inGrow:
  281.             windSize = GrowWindow( whichWindow, gTheEvent.where, &gSizeRect );
  282.             if ( windSize != 0 )
  283.                 {
  284.                 GetPort( &oldPort );
  285.                 SetPort( whichWindow );
  286.                 EraseRect( &whichWindow->portRect );
  287.                 SizeWindow( whichWindow, LoWord( windSize ), HiWord( windSize ), NORMAL_UPDATES );
  288.                 InvalRect( &whichWindow->portRect );
  289.                 SetPort( oldPort );
  290.                 }
  291.             break;
  292.         case inGoAway :
  293.             gDone = TRUE;
  294.             break;
  295.         case inZoomIn:
  296.         case inZoomOut:
  297.             if ( TrackBox( whichWindow, gTheEvent.where, thePart ) )
  298.                 {
  299.                 GetPort( &oldPort );
  300.                 SetPort( whichWindow );
  301.                 EraseRect( &whichWindow->portRect );
  302.                 ZoomWindow( whichWindow, thePart, LEAVE_WHERE_IT_IS );
  303.                 InvalRect( &whichWindow->portRect );
  304.                 SetPort( oldPort );
  305.                 }
  306.             break;
  307.         }
  308. }
  309.  
  310. /******************************** DrawMyPicture *********/
  311.  
  312. DrawMyPicture( drawingWindow, thePicture )
  313.  
  314. WindowPtr    drawingWindow;
  315. PicHandle    thePicture;
  316.  
  317. {
  318.     Rect        drawingClipRect, myRect;
  319.     GrafPtr        oldPort;
  320.     RgnHandle    tempRgn;
  321.     
  322.     GetPort( &oldPort );
  323.     SetPort( drawingWindow );
  324.     tempRgn = NewRgn();
  325.     GetClip( tempRgn );
  326.     EraseRect( &drawingWindow->portRect );
  327.     DrawGrowIcon( drawingWindow );
  328.     drawingClipRect = drawingWindow->portRect;
  329.     drawingClipRect.right -= SCROLL_BAR_PIXELS;
  330.     drawingClipRect.bottom -= SCROLL_BAR_PIXELS;
  331.     
  332.     myRect = drawingWindow->portRect;
  333.     CenterPict( thePicture, &myRect );
  334.     ClipRect( &drawingClipRect );
  335.     DrawPicture( thePicture, &myRect );
  336.     
  337.     SetClip( tempRgn );
  338.     DisposeRgn( tempRgn );
  339.     SetPort( oldPort );
  340. }
  341.  
  342. /******************************** CenterPict *********/
  343.  
  344. CenterPict( thePicture, myRectPtr )
  345.  
  346. PicHandle    thePicture;
  347. Rect        *myRectPtr;
  348.  
  349. {
  350.     Rect    windRect, pictureRect;
  351.     
  352.     windRect = *myRectPtr;
  353.     pictureRect = (**( thePicture )).picFrame;
  354.     myRectPtr->top = (windRect.bottom - windRect.top - (pictureRect.bottom - pictureRect.top)) / 2 + windRect.top;
  355.     myRectPtr->bottom = myRectPtr->top + (pictureRect.bottom - pictureRect.top);
  356.     myRectPtr->left = (windRect.right - windRect.left - (pictureRect.right - pictureRect.left)) / 2 + windRect.left;
  357.     myRectPtr->right = myRectPtr->left + (pictureRect.right - pictureRect.left);
  358. }